<?php
class Admin extends CI_Controller{
	function __construct(){
		parent::__construct();
	}
	
	function index(){
		redirect('/site');
	}
	
	function listuser(){
		$this->load->model('usermodel');
		$type = $this->uri->segment(3);
		$data['title'] = 'List user';
		$data['rows'] = $this->usermodel->listAll();
		$data['check'] = 0;
		switch ($type)
		{
			case 'student':
				$data['rows'] = $this->usermodel->listUser(1);
				$data['check'] = 1;
				break;
			case 'teacher':
				$data['rows'] = $this->usermodel->listUser(2);
				$data['check'] = 2;
				break;
			case 'admin':
				$data['rows'] = $this->usermodel->listUser(3);
				$data['check'] = 3;
				break;
		}
		$this->load->view('admin/listuser',$data);
	}
	
	function addUser(){
		$type = $this->uri->segment(3);
		$data['check'] = 0;
		$data['title'] = 'Create user';
		if ($type == 'student')
			$data['check'] = 1;
		else if ($type == 'teacher')
			$data['check'] = 2;
		else if ($type == 'admin')
			$data['check'] = 3; 
		$data['checkType'] = $this->session->userdata('checkType');
	//	$this->session->set_userdata('checkType',$data['check']);
		$this->load->library('form_validation');
		$config = array(
			array(
				'field' => 'fname',
				'rules' => 'required|trim'
			),
			array(
				'field' => 'lname',
				'rules' => 'required|trim'
			),
			array(
				'field' => 'dOb',
				'rules' => 'required|trim'
			),
			array(
				'field' => 'email',
				'rules' => 'required|trim|valid_email'
			),
			array(
				'field' => 'falculty',
				'rules' => 'required|trim'
			)
		);
		
		//set rules
		$this->form_validation->set_rules($config);

		if ($this->form_validation->run()){
			$this->load->model('usermodel');
			$t = $this->session->userdata('checkType');
			$this->usermodel->add($t);
			redirect('admin/listuser');			
		}else
			$this->load->view('admin/addUser',$data);
	}
	
	function delete(){
		$id = $this->uri->segment(3);
		$this->load->model('usermodel');
		$this->load->model('thesismodel');
		$this->load->model('liststudent');
	//	$this->load->model('profile');
		$this->liststudent->deleteById($id);
	//	$this->profile->deleteByUserId($id);
		$data = $this->thesismodel->getTopic($id);
		foreach ($data as $d){
			if ($d)
				$this->liststudent->deleteId($d->thesis_id);
			else 	
				break;
		}
		$this->thesismodel->deleteByIdTeacher($id);
		$this->usermodel->delete($id);
		redirect('admin/listuser');	
	}
}
?>